package com.woniuxy.controller;

import com.woniuxy.dao.CurrentUnitMapper;
import com.woniuxy.pojo.Dictionary;
import com.woniuxy.pojo.CurrentUnit;
import com.woniuxy.service.CurrentUnitService;
import com.woniuxy.util.DataGridView;
import com.woniuxy.util.ResultDto;
import com.woniuxy.vo.CurrentUnitVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class CurrentUnitController extends BaseController {

    Logger logger= LoggerFactory.getLogger(this.getClass());
    @Autowired
    private CurrentUnitService currentUnitService;
    @Autowired
    private CurrentUnitMapper currentUnitMapper;
    @GetMapping("/currentUnits")
    @ResponseBody
    public DataGridView selectCurrentUnitsByPage(CurrentUnitVo currentUnitVo){
        return currentUnitService.getCurrentUnitListByPage(currentUnitVo);
    }
    @RequestMapping(value = "/toCurrentUnitManager.do")
    public String toCurrentUnitManager(){
        return "currentUnit/currentUnitList";
    }
    @RequestMapping(value = "/toCurrentUnitAddPage.do")
    public String toCurrentUnitAddPage(){
        return "currentUnit/currentUnitAdd";
    }
    @RequestMapping(value = "/toCurrentUnitEditPage.do")
    public String toCurrentUnitEditPage(){
        return "currentUnit/currentUnitEdit";
    }

    @PostMapping(value = "/currentUnits.do")
    @ResponseBody
    public ResultDto addCurrentUnit(CurrentUnitVo currentUnitVo){
        ResultDto dto=new ResultDto();
        CurrentUnit currentUnitResult=this.currentUnitService.findCurrentUnitByPhone(currentUnitVo.getUnit_name());
        if(currentUnitResult!=null){
            return ResultDto.CURRENTUNIT_EXITS;
        }else{
            boolean flag=this.currentUnitService.addCurrentUnit(currentUnitVo);
            if(flag){
                return ResultDto.CURRENTUNIT_ADD_SUCCESS;
            }else{
                return ResultDto.CURRENTUNIT_ADD_FAILURE;
            }
        }
    }

    @PostMapping("/editcurrentUnits.do")
    @ResponseBody
    public ResultDto upadteCurrentUnit(CurrentUnit currentUnit){
        try{
            boolean flag=currentUnitService.update(currentUnit);
            if(flag){
                return ResultDto.UPDATE_CURRENTUNIT_SUCCESS;
            }else{
                return ResultDto.UPDATE_CURRENTUNIT_FAILURE;
            }
        }catch (Exception e){
            e.printStackTrace();
            return ResultDto.UPDATE_CURRENTUNIT_FAILURE;
        }
    }
    @GetMapping(value = "/currentUnit/{id}")
    @ResponseBody
    public ResultDto deleteCurrentUnit(@PathVariable("id") int id){
        try{
            this.currentUnitService.removeCurrentUnit(id);
            return ResultDto.DELETE_CURRENTUNIT_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultDto.DELETE_CURRENTUNIT_FAILURE;
        }
    }

    @RequestMapping(value = "/currentUnit/deleteBatch")
    @ResponseBody
    public ResultDto deleteBathCurrentUnit(@RequestParam("ids[]") Integer[] ids){
        try{
            this.currentUnitService.removeBathCurrentUnit(ids);
            return ResultDto.DELETE_CURRENTUNIT_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultDto.DELETE_CURRENTUNIT_FAILURE;
        }
    }

    @GetMapping(value = "/currentUnit/currentUnittypes")
    @ResponseBody
    public List<Dictionary> currentUnittypes(){
        return currentUnitMapper.selectAllCurrentUnitTypes();
    }

}
